<html>
<head><meta charset="utf-8"><title>C error code in Rust · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html">C error code in Rust</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="137034023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137034023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137034023">(Nov 02 2018 at 14:19)</a>:</h4>
<p>Is there an idiom to represent C error codes in Rust C FFI? I have a C function that returns a <code>libc::c_int</code>, where <code>0</code> indicates success, and the rest are error conditions. </p>
<p>I was thinking of representing the error conditions as <code>#[repr(C, transparent)] struct Error(pub NonZero&lt;c_int&gt;);</code>, and then using <code>Result&lt;???, Error&gt;</code> in C FFI, the question is, what do I put in <code>()</code> so that <code>0</code> gets translated to <code>Ok</code> ?</p>



<a name="137034262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137034262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137034262">(Nov 02 2018 at 14:19)</a>:</h4>
<p>Or shall I just use <code>Option&lt;Error&gt;</code> as the return type? (that should map <code>0</code> to <code>None</code>)</p>



<a name="137037020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137037020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137037020">(Nov 02 2018 at 14:30)</a>:</h4>
<p>Damn, NonZero&lt;T&gt; does not exist anymore - how can I dispatch to the appropriate <code>NonZeroU{X}</code> type from <code>c_int</code> ?</p>



<a name="137038060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137038060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137038060">(Nov 02 2018 at 14:34)</a>:</h4>
<p>how unstable are you willing to go?</p>



<a name="137038332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137038332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137038332">(Nov 02 2018 at 14:35)</a>:</h4>
<p>We do have <a href="https://github.com/rust-lang/rust/blob/6310be458f4665f537419f033a764b0644c7e5ab/src/libcore/nonzero.rs#L17" target="_blank" title="https://github.com/rust-lang/rust/blob/6310be458f4665f537419f033a764b0644c7e5ab/src/libcore/nonzero.rs#L17">this attribute</a> which allows compiler internals to invent <code>NonZero</code></p>



<a name="137038572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137038572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137038572">(Nov 02 2018 at 14:36)</a>:</h4>
<p>(and it will never be stabilized)</p>



<a name="137038620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137038620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137038620">(Nov 02 2018 at 14:36)</a>:</h4>
<p>Thanks, that might do. I feel it really weird that nobody raised "how do we use <code>NonZero</code> for libc types in the RFC that decided to kill NonZero&lt;T&gt; =/</p>



<a name="137038801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137038801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137038801">(Nov 02 2018 at 14:37)</a>:</h4>
<p>well... I think the idea is to have integer subtypes at some point. So that you can say things like <code>i32&lt;5..30&gt;</code> or whatever bikeshedded syntax we get</p>



<a name="137039963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137039963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137039963">(Nov 02 2018 at 14:41)</a>:</h4>
<p>the second problem is that result is not FFI safe</p>



<a name="137040805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137040805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137040805">(Nov 02 2018 at 14:45)</a>:</h4>
<p>well... I'd use <code>Option</code> in that case.</p>



<a name="137040915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137040915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137040915">(Nov 02 2018 at 14:45)</a>:</h4>
<p>Or create an <code>FfiResult</code></p>



<a name="137041543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137041543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137041543">(Nov 02 2018 at 14:47)</a>:</h4>
<p>Is there a way to easily create an <code>FfiResult</code> type that has the same API as <code>Result</code> ?</p>



<a name="137041699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137041699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137041699">(Nov 02 2018 at 14:48)</a>:</h4>
<blockquote>
<p>how can I dispatch to the appropriate NonZeroU{X} type from c_int ?</p>
</blockquote>
<p>maybe you could</p>
<div class="codehilite"><pre><span></span><span class="k">trait</span><span class="w"> </span><span class="n">HasNonZeroUnsigned</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">NonZeroUnsigned</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">HasNonZeroUnsigned</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">NonZeroUnsigned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">NonZeroU32</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">HasNonZeroUnsigned</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">isize</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">NonZeroUnsigned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">NonZeroUsize</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">...</span><span class="w"></span>
<span class="k">type</span> <span class="nc">NonZeroCUInt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&lt;</span><span class="n">c_int</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">HasNonZeroUnsigned</span><span class="o">&gt;</span>::<span class="n">NonZeroUnsigned</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="137041825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/C%20error%20code%20in%20Rust/near/137041825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/C.20error.20code.20in.20Rust.html#137041825">(Nov 02 2018 at 14:48)</a>:</h4>
<p>that's a good idea <span class="user-mention" data-user-id="125254">@kennytm</span>  :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>